package com.shijie.controller.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.AppUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.domain.entity.Magazines;
import com.shijie.domain.entity.UserAttentions;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.service.MagazinesService;
import com.shijie.service.UserAttentionsService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;
/*
 *  资讯，线下活动，达人文章
 *      相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 *
 */
@RestController
@RequestMapping("/app/magazines")
public class AppMagazinesController {
    @Resource
    private MagazinesService magazinesService;
    @Resource
    private AppUserService appUserService;
    @Resource
    private UserAttentionsService userAttentionsService;


    /**
     * 咨询类信息查询
     *
     * @param page
     * @param pageSize
     * @param q
     * @param categoryId
     * @param type  实验室 LABORATORY , 热门 HOT , 普通资讯 ORDINARY
     *
     * @return
     */
	@GetMapping(value = "index")
	public Result searchForIndex(@RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String q,
			@RequestParam(required = false) Integer categoryId,
			@RequestParam(required = false) EnumInterface.EntityType type) {
		PageHelper.startPage(page, pageSize);
		List<Magazines> list = magazinesService.searchForIndex(q, categoryId, EnumInterface.Status.VALID.ordinal(),
				type);
		PageInfo<Magazines> pageInfo = new PageInfo<>(list);
		userAttentionsService.attentions(appUserService.getCurrentUser().getId(), pageInfo.getList());
		return ResultGenerator.success(pageInfo);
	}


    /**
     * 达人的文章查询
     *
     * @param page
     * @param pageSize
     * @param q
     * @return
     */
	@GetMapping(value = "talent")
	public Result searchForTalent(@RequestParam(defaultValue = "0") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) String q) {
		PageHelper.startPage(page, pageSize);
		List<Magazines> list = magazinesService.searchForTalent(q, EnumInterface.Status.VALID.ordinal());
		PageInfo<Magazines> pageInfo = new PageInfo<>(list);
		userAttentionsService.attentions(appUserService.getCurrentUser().getId(), pageInfo.getList());
		return ResultGenerator.success(pageInfo);
	}

    /**
     * 获取详情信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "detail/{id}")
	public Result detail(@PathVariable Integer id) {
		Magazines magazines = magazinesService.selectById(id);
		if (magazines != null) {
			List<Magazines> list = new ArrayList<Magazines>();
			list.add(magazines);
			userAttentionsService.attentions(appUserService.getCurrentUser().getId(), list);
			return ResultGenerator.success(list.get(0));
		} else {
			return ResultGenerator.error("无资讯ID为：" + id + "的内容");
		}

	}

    /**
     * 分享，增加对应的资讯，达人文章的分享次数
     *
     * @param id
     * @return
     */
    @GetMapping(value = "share/{id}")
    public Result share(@PathVariable Integer id) {
        if(magazinesService.share(id)){
            return ResultGenerator.success();
        }else{
            return ResultGenerator.error("分享失败，没有对应的资讯或者达人文章！");
        }

    }

}
